1
|
|
|
/** global: wp, pollux, CodeMirror */ |
2
|
|
|
|
3
|
|
|
pollux.dependency = {}; |
4
|
|
|
pollux.editors = {}; |
5
|
|
|
pollux.featured = {}; |
6
|
|
|
pollux.metabox = {}; |
7
|
|
|
pollux.tabs = {}; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @return bool |
11
|
|
|
*/ |
12
|
|
|
pollux.classListAction = function( bool ) |
13
|
|
|
{ |
14
|
|
|
return bool ? 'add' : 'remove'; |
15
|
|
|
}; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
pollux.dependency.ajax = function( action, el, type ) |
21
|
|
|
{ |
22
|
|
|
var args = pollux.dependency.getAjaxOptions( el ); |
23
|
|
|
wp.ajax.send( action, { |
24
|
|
|
_ajax_nonce: wp.updates.ajaxNonce, |
25
|
|
|
error: args.error, |
26
|
|
|
plugin: args.plugin, |
27
|
|
|
success: args.success, |
28
|
|
|
type: type, |
29
|
|
|
}); |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return object |
34
|
|
|
*/ |
35
|
|
|
pollux.dependency.getAjaxOptions = function( el ) |
36
|
|
|
{ |
37
|
|
|
return { |
38
|
|
|
error: pollux.dependency.onError.bind( el ), |
39
|
|
|
plugin: el.getAttribute( 'data-plugin' ), |
40
|
|
|
slug: el.getAttribute( 'data-slug' ), |
41
|
|
|
success: pollux.dependency.onSuccess.bind( el ), |
42
|
|
|
}; |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
pollux.dependency.init = function() |
49
|
|
|
{ |
50
|
|
|
pollux.dependency.buttons = document.querySelectorAll( '.pollux-notice a.button' ); |
51
|
|
|
[].forEach.call( pollux.dependency.buttons, function( button ) { |
52
|
|
|
button.addEventListener( 'click', pollux.dependency.onClick ); |
53
|
|
|
}); |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
pollux.dependency.install = function( el, args ) |
60
|
|
|
{ |
61
|
|
|
pollux.dependency.updateButtonText( el, 'pluginInstallingLabel' ); |
62
|
|
|
el.classList.add( 'updating-message' ); |
63
|
|
|
return wp.updates.ajax( 'install-plugin', args ); |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
pollux.dependency.onClick = function( ev ) |
70
|
|
|
{ |
71
|
|
|
var action = this.href.match(/action=([^&]+)/); |
72
|
|
|
if( action === null )return; |
|
|
|
|
73
|
|
|
action = action[1].split('-')[0]; |
74
|
|
|
if( !pollux.dependency[action] )return; |
|
|
|
|
75
|
|
|
this.blur(); |
76
|
|
|
ev.preventDefault(); |
77
|
|
|
if( this.classList.contains( 'updating-message' ))return; |
|
|
|
|
78
|
|
|
pollux.dependency[action]( this, pollux.dependency.getAjaxOptions( this )); |
79
|
|
|
}; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
pollux.dependency.onError = function( response ) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
window.location = this.href; |
87
|
|
|
}; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
pollux.dependency.onSuccess = function( response ) |
93
|
|
|
{ |
94
|
|
|
var type = response.install ? 'install' : 'update'; |
95
|
|
|
if( !response.activate_url ) { |
96
|
|
|
return pollux.dependency.ajax( 'pollux/dependency/activate_url', this, type ); |
97
|
|
|
} |
98
|
|
|
pollux.dependency.setUpdatedMessage( this, type ); |
99
|
|
|
if( response.activate_url ) { |
|
|
|
|
100
|
|
|
setTimeout( _.bind( pollux.dependency.setActivateButton( this, response ), this ), 1000 ); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
}; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
pollux.dependency.setActivateButton = function( el, response ) |
108
|
|
|
{ |
109
|
|
|
el.classList.remove( 'updated-message' ); |
110
|
|
|
el.classList.remove( 'button-disabled' ); |
111
|
|
|
pollux.dependency.updateButtonText( el, 'activatePluginLabel' ); |
112
|
|
|
el.classList.add( 'button-primary' ); |
113
|
|
|
el.href = response.activateUrl; |
114
|
|
|
}; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
pollux.dependency.setUpdatedMessage = function( el, type ) |
120
|
|
|
{ |
121
|
|
|
el.classList.remove( 'updating-message' ); |
122
|
|
|
el.classList.add( 'updated-message' ); |
123
|
|
|
el.classList.add( 'button-disabled' ); |
124
|
|
|
pollux.dependency.updateButtonText( el, ( |
125
|
|
|
type === 'install' ? 'pluginInstalledLabel' : 'updatedLabel' |
126
|
|
|
)); |
127
|
|
|
}; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
pollux.dependency.updateButtonText = function( el, l10nkey ) |
133
|
|
|
{ |
134
|
|
|
if( !wp.updates.l10n[l10nkey] )return; |
|
|
|
|
135
|
|
|
var label = wp.updates.l10n[l10nkey].replace( '%s', el.getAttribute( 'data-name' )); |
136
|
|
|
if( el.innerHTML !== label ) { |
137
|
|
|
el.innerHTML = label; |
138
|
|
|
} |
139
|
|
|
}; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
pollux.dependency.upgrade = function( el, args ) |
145
|
|
|
{ |
146
|
|
|
pollux.dependency.updateButtonText( el, 'updatingLabel' ); |
147
|
|
|
el.classList.add( 'updating-message' ); |
148
|
|
|
return wp.updates.ajax( 'update-plugin', args ); |
149
|
|
|
}; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
pollux.editors.disable = function( index ) |
155
|
|
|
{ |
156
|
|
|
pollux.editors.all[index].setOption( 'theme', 'disabled' ); |
157
|
|
|
pollux.editors.all[index].setOption( 'readOnly', 'nocursor' ); |
158
|
|
|
}; |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
pollux.editors.enable = function( index ) |
164
|
|
|
{ |
165
|
|
|
pollux.editors.all[index].setOption( 'theme', 'pollux' ); |
166
|
|
|
pollux.editors.all[index].setOption( 'readOnly', false ); |
167
|
|
|
}; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
pollux.editors.init = function() |
173
|
|
|
{ |
174
|
|
|
pollux.editors.all = []; |
175
|
|
|
[].forEach.call( document.querySelectorAll( '.pollux-code' ), function( editor, index ) { |
176
|
|
|
pollux.editors.all[index] = CodeMirror.fromTextArea( editor, { |
177
|
|
|
gutters: ['CodeMirror-lint-markers'], |
178
|
|
|
highlightSelectionMatches: { wordsOnly: true }, |
179
|
|
|
lineNumbers: true, |
180
|
|
|
lint: true, |
181
|
|
|
mode: 'text/yaml', |
182
|
|
|
showInvisibles: true, |
183
|
|
|
showTrailingSpace: true, |
184
|
|
|
styleActiveLine: true, |
185
|
|
|
tabSize: 2, |
186
|
|
|
theme: 'pollux', |
187
|
|
|
viewportMargin: Infinity, |
188
|
|
|
}); |
189
|
|
|
pollux.editors.all[index].setOption( 'extraKeys', { |
190
|
|
|
Tab: function( cm ) { |
191
|
|
|
var spaces = Array( cm.getOption( 'indentUnit' ) + 1 ).join( ' ' ); |
192
|
|
|
cm.replaceSelection( spaces ); |
193
|
|
|
}, |
194
|
|
|
}); |
195
|
|
|
pollux.editors.all[index].display.wrapper.setAttribute( 'data-disabled', editor.getAttribute( 'data-disabled' )); |
196
|
|
|
if( editor.readOnly ) { |
197
|
|
|
pollux.editors.disable( index ); |
198
|
|
|
} |
199
|
|
|
}); |
200
|
|
|
}; |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
pollux.featured.init = function() |
206
|
|
|
{ |
207
|
|
|
jQuery( '#postimagediv' ) |
208
|
|
|
.on( 'click', '#pollux-set-featured', function( ev ) { |
209
|
|
|
ev.preventDefault(); |
210
|
|
|
wp.media.view.settings.post.featuredImageId = Math.round( jQuery( '#featured' ).val() ); |
211
|
|
|
pollux.featured.frame = wp.media.featuredImage.frame; |
212
|
|
|
pollux.featured.frame().open(); |
213
|
|
|
}) |
214
|
|
|
.on( 'click', '#pollux-remove-featured', function( ev ) { |
215
|
|
|
ev.preventDefault(); |
216
|
|
|
pollux.featured.set(-1); |
217
|
|
|
}); |
218
|
|
|
}; |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return void |
222
|
|
|
*/ |
223
|
|
|
pollux.featured.select = function() |
224
|
|
|
{ |
225
|
|
|
if( !wp.media.view.settings.post.featuredImageId )return; |
226
|
|
|
var selection = this.get( 'selection' ).single(); |
227
|
|
|
pollux.featured.set( selection ? selection.id : -1 ); |
228
|
|
|
}; |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return void |
232
|
|
|
*/ |
233
|
|
|
pollux.featured.set = function( id ) |
234
|
|
|
{ |
235
|
|
|
wp.media.view.settings.post.featuredImageId = Math.round( id ); |
236
|
|
|
wp.media.post( 'pollux/archives/featured/html', { |
237
|
|
|
_wpnonce: document.querySelector( '#_wpnonce' ).value, |
238
|
|
|
post_type: document.querySelector( '#archive-type' ).value, |
239
|
|
|
thumbnail_id: id, |
240
|
|
|
}).done( function( html ) { |
241
|
|
|
document.querySelector( '#postimagediv > .inside' ).innerHTML = html; |
242
|
|
|
}); |
243
|
|
|
}; |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
|
|
pollux.metabox.hasValue = function( el ) |
249
|
|
|
{ |
250
|
|
|
if( el.type === 'checkbox' ) { |
251
|
|
|
return el.checked === true; |
252
|
|
|
} |
253
|
|
|
return el.value !== ''; |
254
|
|
|
}; |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return void |
258
|
|
|
*/ |
259
|
|
|
pollux.metabox.init = function() |
260
|
|
|
{ |
261
|
|
|
var depends = document.querySelectorAll( '.rwmb-input [data-depends]' ); |
262
|
|
|
[].forEach.call( depends, function( el ) { |
263
|
|
|
var dependency = pollux.metabox.setVisibility( el ); |
264
|
|
|
var event = dependency.type === 'checkbox' ? 'change' : 'keyup'; |
265
|
|
|
dependency.addEventListener( event, function() { |
266
|
|
|
pollux.metabox.setVisibility( el ); |
267
|
|
|
}); |
268
|
|
|
}); |
269
|
|
|
}; |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return element |
273
|
|
|
*/ |
274
|
|
|
pollux.metabox.setVisibility = function( el ) |
275
|
|
|
{ |
276
|
|
|
var dependency = document.getElementById( el.getAttribute( 'data-depends' )); |
277
|
|
|
var action = pollux.classListAction( !pollux.metabox.hasValue( dependency )); |
278
|
|
|
el.closest( '.rwmb-field' ).classList[action]( 'hidden' ); |
279
|
|
|
return dependency; |
280
|
|
|
}; |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
|
|
pollux.tabs.init = function() |
286
|
|
|
{ |
287
|
|
|
pollux.tabs.active = document.querySelector( '#pollux-active-tab' ); |
288
|
|
|
pollux.tabs.referrer = document.querySelector( 'input[name="_wp_http_referer"]' ); |
289
|
|
|
pollux.tabs.tabs = document.querySelectorAll( '.pollux-tabs a' ); |
290
|
|
|
pollux.tabs.views = document.querySelectorAll( '.pollux-config .form-table' ); |
291
|
|
|
|
292
|
|
|
[].forEach.call( pollux.tabs.tabs, function( tab, index ) { |
293
|
|
|
var active = location.hash ? tab.getAttribute( 'href' ).slice(1) === location.hash.slice(2) : index === 0; |
294
|
|
|
if( active ) { |
295
|
|
|
pollux.tabs.setTab( tab ); |
296
|
|
|
} |
297
|
|
|
tab.addEventListener( 'click', pollux.tabs.onClick ); |
298
|
|
|
tab.addEventListener( 'touchend', pollux.tabs.onClick ); |
299
|
|
|
}); |
300
|
|
|
}; |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return void |
304
|
|
|
*/ |
305
|
|
|
pollux.tabs.onClick = function( ev ) |
306
|
|
|
{ |
307
|
|
|
ev.preventDefault(); |
308
|
|
|
this.blur(); |
309
|
|
|
pollux.tabs.setTab( this ); |
310
|
|
|
location.hash = '!' + this.getAttribute( 'href' ).slice(1); |
311
|
|
|
// pollux.editors.all.forEach( function( editor ) { |
312
|
|
|
// editor.refresh(); |
313
|
|
|
// }); |
314
|
|
|
}; |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return void |
318
|
|
|
*/ |
319
|
|
|
pollux.tabs.setReferrer = function( index ) |
320
|
|
|
{ |
321
|
|
|
var referrerUrl = pollux.tabs.referrer.value.split('#')[0] + '#!' + pollux.tabs.views[index].id; |
322
|
|
|
pollux.tabs.referrer.value = referrerUrl; |
323
|
|
|
}; |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return void |
327
|
|
|
*/ |
328
|
|
|
pollux.tabs.setTab = function( el ) |
329
|
|
|
{ |
330
|
|
|
[].forEach.call( pollux.tabs.tabs, function( tab, index ) { |
331
|
|
|
var action = pollux.classListAction( tab === el ); |
332
|
|
|
if( action === 'add' ) { |
333
|
|
|
pollux.tabs.active.value = pollux.tabs.views[index].id; |
334
|
|
|
pollux.tabs.setReferrer( index ); |
335
|
|
|
pollux.tabs.setView( index ); |
336
|
|
|
} |
337
|
|
|
tab.classList[action]( 'nav-tab-active' ); |
338
|
|
|
}); |
339
|
|
|
}; |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return void |
343
|
|
|
*/ |
344
|
|
|
pollux.tabs.setView = function( idx ) |
345
|
|
|
{ |
346
|
|
|
[].forEach.call( pollux.tabs.views, function( view, index ) { |
347
|
|
|
var action = pollux.classListAction( index !== idx ); |
348
|
|
|
view.classList[action]( 'ui-tabs-hide' ); |
349
|
|
|
}); |
350
|
|
|
}; |
351
|
|
|
|
352
|
|
|
jQuery(function() { |
353
|
|
|
for( var key in pollux ) { |
|
|
|
|
354
|
|
|
if( !pollux[key].hasOwnProperty( 'init' ))continue; |
|
|
|
|
355
|
|
|
pollux[key].init(); |
356
|
|
|
} |
357
|
|
|
}); |
358
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.